package controller;

import dao.UserMapper;
import entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Reader;

/**
 * Created by zjh on 2020/11/17 下午 3:33
 */
@WebServlet("/registerController")
public class registerController extends HttpServlet {
    @Override
    public void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
            throws ServletException, IOException {
        //解决乱码问题↓
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setCharacterEncoding("UTF-8");
        servletResponse.setContentType("text/html; charset=UTF-8");

        String email = servletRequest.getParameter("email");//邮箱地址
        String user_name = servletRequest.getParameter("user_name");//用户名
        String password = servletRequest.getParameter("password");//密码
        //String repassword = servletRequest.getParameter("repassword");//确定密码
        String gender = servletRequest.getParameter("gender");//性别
        String telephone = servletRequest.getParameter("telephone");//电话号码
        String introduce = servletRequest.getParameter("introduce");//个人介绍

        //用set方法把前端获取的数据塞到user对象里
        User user = new User();
        user.setUsername(user_name);
        user.setEmail(email);
        user.setPassword(password);
        user.setGender(gender);
        user.setTelephone(telephone);
        user.setIntrouduce(introduce);

        //测试获取到前端的到数据
        //System.out.println(user.getUsername());

        //调用insertUser方法，传入user对象
        insertUser(user);

        //判断用户是否注册成功
        if(result!=0){
            //servletResponse.getWriter().write("注册成功");
            //注册成功后0.1秒自动跳转到注册成功页面
            servletResponse.setHeader("refresh",
     "0.1;url=http://localhost:8080/CZBKBookshop_war_exploded/client/registersuccess.html");
        }else {
            servletResponse.getWriter().write("注册失败，用户名存在");
            //注册失败后3秒自动跳转到注册页面
            servletResponse.setHeader("refresh", "3;url=http://localhost:63342/CZBKBookshop/web/client/register.html");
        }
        }
    static int result;//静态变量
    public static void insertUser(User user) throws IOException {
        try {
            //使用MyBatis提供的Resources类加载mybatis的配置文件
            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
            //构建sqlSession的工厂
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);

            SqlSession session= sessionFactory.openSession();
            //加载sql映射
            UserMapper mapper=session.getMapper(UserMapper.class);
            result=mapper.insertUser(user);
            if (result!=0){
                session.commit();
                System.out.println(user.getUsername()+"注册成功");
            }else {
                System.out.println(user.getUsername()+"注册失败");
            }
            session.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
